package com.lxy.user.task;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxy.common.enums.MailTitle;
import com.lxy.user.domain.po.LogStatus;
import com.lxy.user.domain.po.User;
import com.lxy.user.service.LogStatusService;
import com.lxy.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class LogTask {

    @Autowired
    private LogStatusService logStatusService;
    @Autowired
    private UserService userService;

    @Scheduled(cron = "0 0 1 * * MON-FRI")
    public void CleanRegularly(){
        //工作日凌晨一点重置log_status表中的status
        List<LogStatus> logStatuses = logStatusService.list();
        //status置零
        for (LogStatus logStatus : logStatuses) {
            logStatus.setStatus(0);
        }
        //更新数据
        logStatusService.updateBatchById(logStatuses);
    }

    //工作日下午6点发邮件提醒没有上交今日维护日志的维修人员
    @Scheduled(cron = "0 0 18 ? * MON-FRI")
    public void ReminderToTurnInLog() throws Exception {
        //查找没提交日志的维修人员
        LambdaQueryWrapper<LogStatus> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LogStatus::getStatus,0);
        //获取人员的id
        List<LogStatus> list = logStatusService.list(lambdaQueryWrapper);
        for (LogStatus logStatus : list) {
            Long userId = logStatus.getUserId();
            //查找人员的邮箱
            User user = userService.getById(userId);
            String email = user.getEmail();
            //发送邮件提醒
            userService.sendMail(MailTitle.TURN_IN_LOG,"请在20：00之前提交今日维护日志",email);
        }
    }
}
